package com.sxdz.controller.handler

import groovyx.net.http.HTTPBuilder
import org.apache.http.entity.StringEntity

/**
 * 接口请求处理handler
 * Created by lzw on 2016-8-17.
 */
class RequestRemoteHandler {
    static String UserAgent = "Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4"
    /**
     * Post方式请求
     * @param host
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    static Object post(String url,Map<String,String> param,Map<String,String> headParam, String content) throws Exception{
        def result
        if (headParam==null) headParam =[:]
        headParam << [UserAgent:UserAgent]

        def http = new HTTPBuilder(url)
        http.request(groovyx.net.http.Method.POST,groovyx.net.http.ContentType.ANY) {req->
            uri.query =param
            headers = headParam
            if(content!=null)
                req.entity = new StringEntity(content,"UTF-8")

            //请求成功
            response.success = { resp, json ->
                result = json
            }

            response.failure = { resp ->
                println "内部接口通讯异常: ${resp.status} : ${resp.statusLine.reasonPhrase}"
            }

        }
        return result
    }

    /**
     * Get方式请求
     * @param host
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    static Object get(String url,Map<String,String> param,Map<String,String> headParam) throws Exception{
        def result
        if (headParam==null) headParam =[:]
        headParam << [UserAgent:UserAgent]

        def http =new HTTPBuilder(url)
        http.request(groovyx.net.http.Method.GET,groovyx.net.http.ContentType.ANY) {
            uri.query =param

            headers = headParam
            //请求成功
            response.success = { resp, json ->
                result = json
            }

            response.failure = { resp ->
                println "内部接口通讯异常: ${resp.status} : ${resp.statusLine.reasonPhrase}"
                throw new Exception("9002")
            }

        }
        return result
    }

    /**
     * Post方式请求
     * @param host
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    static Object postJson(String url,Map<String,String> param,Map<String,String> headParam, String content) throws Exception{
        def result
        if (headParam==null) headParam =[:]
        headParam << [UserAgent:UserAgent]

        def http = new HTTPBuilder(url)
        http.request(groovyx.net.http.Method.POST,groovyx.net.http.ContentType.JSON) { req->
            uri.query =param
            headers = headParam
            if(content!=null)
                req.entity = new StringEntity(content,"UTF-8")
            //请求成功
            response.success = { resp, json ->
                result = json
            }

            response.failure = { resp ->
                println "内部接口通讯异常: ${resp.status} : ${resp.statusLine.reasonPhrase}"

                //result['${resp.status}']='${resp.statusLine.reasonPhrase}'
            }

        }
        return result
    }

    /**
     * Get方式请求
     * @param host
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    static Object getJson(String url,Map<String,String> param,Map<String,String> headParam) throws Exception{
        def result
        if (headParam==null) headParam =[:]
        headParam << [UserAgent:UserAgent]

        def http =new HTTPBuilder(url)
        http.request(groovyx.net.http.Method.GET,groovyx.net.http.ContentType.JSON) {
            uri.query =param

            headers = headParam
            //请求成功
            response.success = { resp, json ->
                result = json
            }

            response.failure = { resp ->
                println "内部接口通讯异常: ${resp.status} : ${resp.statusLine.reasonPhrase}"
                throw new Exception("9002")
            }

        }
        return result
    }

}



